package com.css.fxfzaqbz.modules.assess.controller;

import com.css.fxfzaqbz.base.response.RestResponse;
import com.css.fxfzaqbz.base.upload.entity.AttachmentInfoEntity;
import com.css.fxfzaqbz.base.upload.service.AttachmentInfoService;
import com.css.fxfzaqbz.modules.assess.entity.DisasterAssEntity;
import com.css.fxfzaqbz.modules.assess.entity.RiskAssessmentEntity;
import com.css.fxfzaqbz.modules.assess.service.DisasterService;
import com.css.fxfzaqbz.modules.assess.vo.DisasterAssVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author y1fan
 * @create 2022-09-09 15:59
 * 房屋地震灾害风险
 */

@RestController
@RequestMapping("/fxfzaqbz/data")
@Slf4j
public class DisasterAssController {

    @Resource
    private DisasterService disasterService;
    @Resource
    private AttachmentInfoService attachmentInfoService;



    /*
    *
    * 添加成果上传数据
    * */
    @PostMapping("/saveDisasterAss")
    public RestResponse saveDisasterAss(@RequestBody DisasterAssEntity disasterAssEntity) {
        RestResponse restResponse = null ;
        try{
            DisasterAssEntity disasterAssEntity1 = disasterService.saveDisasterAss(disasterAssEntity);
            restResponse = RestResponse.succeed(disasterAssEntity1);
        }catch (Exception e){
            log.error("新增失败",e);
            restResponse = RestResponse.fail("新增失败!");
        }
        return restResponse;
    }



    /*
    * 过程稿页 接口
    *
    * */
    @GetMapping("/getBigDisaster")
    public RestResponse getBigDisaster(String id){
        try {
          List<DisasterAssEntity> allDisasterAss = disasterService.getAllDisasterAss(id);
            return RestResponse.succeed(allDisasterAss);
        } catch (Exception e) {
            log.error("查看失败",e);
            return RestResponse.fail("查看失败");
        }
    }



    /*
    * 最终成果页 接口
    * */
    @GetMapping("/getBigDisasterTwo")
    public RestResponse getBigDisasterTwo(String id){
        try {
            List<DisasterAssEntity> allDisasterAss = disasterService.getBigDisasterTwo(id);
            return RestResponse.succeed(allDisasterAss);
        } catch (Exception e) {
            log.error("查看失败",e);
            return RestResponse.fail("查看失败");
        }
    }





    /*
    * 根据ID查询成果上传数据
    * */
    @GetMapping("/getDisasterAssById")
    public RestResponse<RiskAssessmentEntity> getDisasterAssById(String id) {
        RestResponse restResponse = null;
        try{
            DisasterAssEntity disasterAssEntity = disasterService.getDisasterAssById(id);
            String sraAssFileId = disasterAssEntity.getSdaAssFileId();
            AttachmentInfoEntity attach = attachmentInfoService.getAttach(sraAssFileId);
            Map<String,Object> map=new HashMap<>();
            map.put("disasterAssEntity",disasterAssEntity);
            map.put("attach",attach);
            restResponse = RestResponse.succeed(map);
        }catch (Exception e){
            log.error("获取数据失败!",e);
            restResponse = RestResponse.fail("获取数据失败!");
        }
        return restResponse;
    }

    /*
    * 删除成果上传数据
    * */
    @PostMapping("/deleteDisasterAss")
    public RestResponse deleteDisasterAss(String id){
        try {
            disasterService.deleteDisasterAss(id);
            return RestResponse.succeed("删除成功");
        } catch (Exception e) {
            log.error("删除失败",e);
            return RestResponse.fail("删除失败");
        }
    }
    /*
    * 修改成果上传数据接口
    * */
    @PostMapping("/updateDisasterAss")
    public RestResponse updateDisasterAss(@RequestBody DisasterAssEntity disasterAssEntity){
        RestResponse restResponse = null;
        try {
            DisasterAssEntity disasterAssEntity1=  disasterService.updateDisasterAssD(disasterAssEntity);
            restResponse=RestResponse.succeed(disasterAssEntity1);
        } catch (Exception e) {
            log.error("修改失败",e);
            return RestResponse.fail("修改失败");
        }
        return restResponse;
    }

    /*
    * 过程稿提交到最终成果接口
    * */
    @PostMapping("/firstSubmitDisasterAss")
    public RestResponse firstSubmitDisasterAss(String id){
        try {
            disasterService.firstSubmitDisasterAss(id);
            return RestResponse.succeed("提交成功");
        } catch (Exception e) {
            log.error("提交失败",e);
            return RestResponse.fail("提交失败");
        }
    }


    /*
    * 是否重复接口
    * */
    @PostMapping("/ifRepetition")
    public RestResponse ifRepetition(@RequestBody DisasterAssEntity disasterAssEntity){
        try {
            Boolean flag=disasterService.ifRepetition(disasterAssEntity);
            return RestResponse.succeed(flag);
        } catch (Exception e) {
            log.error("校验失败",e);
            return RestResponse.fail("校验失败");
        }
    }


    /*
    * 添加描述接口
    * */
    @PostMapping("/addDescribe")
    public RestResponse addDescribe(@RequestBody DisasterAssVO disasterAssVO){
        try {
            String describe="";
            if (disasterAssVO.getDisasterAss()!=null && disasterAssVO.getDisasterAss().size()!=0){
                describe = StringUtils.join(disasterAssVO.getDisasterAss(), "@@@");
            }
           disasterService.addDescribe(disasterAssVO.getIds(),describe);
            return RestResponse.succeed("添加描述成果");
        } catch (Exception e) {
            log.error("添加描述失败",e);
            return RestResponse.fail("添加描述失败");
        }
    }

    /*
    * 移除接口
    * */
    @PostMapping("/removeDisasterAss")
    public RestResponse removeDisasterAss(String id){
        try {
            disasterService.removeDisasterAss(id);
            return RestResponse.succeed("移除成功");
        } catch (Exception e) {
            log.error("移除失败",e);
            return RestResponse.fail("移除失败");
        }
    }


    /*
     * 最终成果提交接口
     * */
    @PostMapping("/secondSubmitDisasterAss/{ids}")
    public RestResponse secendSubmitDisasterAss(@PathVariable("ids") List<String> ids){
        try {
            disasterService.secendSubmitDisasterAss(ids);
            return RestResponse.succeed("提交成功");
        } catch (Exception e) {
            log.error("提交失败",e);
            return RestResponse.fail("提交失败");
        }
    }
}
